
// 这个是为没有aid的用户准备的可以选择图书馆的登录入口，待完善。
Page({

  /**
   * 页面的初始数据
   */
  data: {
    aid: '',
    alertShow: false,
    reader_id: '',
    password: '',
    reader_openid: '',
    reader_id: '',
    rules: [],

  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    this.setData({
      aid: options.aid
    });
  },
  showAlert: function () {
    this.setData({
      alertShow: !this.data.alertShow
    });
  },
  showAlert_rules: function () {
    const that = this;
    if (that.data.rules.length == 0) {
      wx.request({
        url: 'https://2016.bookgo.com.cn/wxapp/index/get_public_rule',
        data: {},
        header: {
          'content-type': 'application/json'
        },
        success: function (res) {
          console.log(res.data);
          var data = res.data;
          var items = []
          for (var i = 0; i < data.length; i++) {
            var item = {
              text: data[i].text
            };
            items.push(item);
          }
          console.log(items)
          that.setData({
            rules: items
          });
        },
        fail: function (res) {

        },
        complete: function () {

        }
      });
    }

    // 弹出框显示
    this.setData({
      alertShow: true
    });
  },
  bindBind: function (e) {
    var formId = e.detail.formId;
    const that = this;
    wx.request({
      url: 'https://2016.bookgo.com.cn/wxapp/api/public_login',
      data: {
        stuid: that.data.reader_id,
        password: that.data.password,
        aid: that.data.aid
      },
      method: 'POST',
      success: function (res) {
        console.log(res);
        if (res.data.code == 101) {
          that.setData({
            reader_id: res.data.data.reader_id,
            reader_openid: res.data.data.openid
          });
          wx.setStorageSync('reader_user', res.data.data);
          that.public_bind();
        } else {
          wx.showModal({
            title: '提示',
            content: res.data.data,
            showCancel: false,
            success: function (res) {
              console.log(res);
            }
          })
        }
      }
    })
  },
  public_bind() {
    const that = this;
    var openid = wx.getStorageSync('openid');
    console.log(openid);
    wx.request({
      url: 'https://2016.bookgo.com.cn/wxapp/api/public_bind',
      data: {
        openid: openid,
        aid: that.data.aid,
        reader_id: that.data.reader_id,
        reader_openid: that.data.reader_openid
      },
      success: function (res) {
        if (res.data.code == 101) {

          wx.setStorageSync('user', res.data.msg);
          wx.redirectTo({
            url: '/bookgo/borrow/borrow?aid=' + that.data.aid,
          })
        } else {
          wx.showModal({
            title: '提示',
            content: res.data.msg,
            showCancel: false,
            success: function (res) {
              console.log(res);
            }
          })
        }
      }
    });

  },
  bindStuidInput: function (e) {
    this.setData({
      reader_id: e.detail.value
    });
  },
  bindPasswordInput: function (e) {
    this.setData({
      password: e.detail.value
    });
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  }
})